// 导入数据库连接对象
const db = require("../db");
// 导入配置信息
const { debug, info } = require("../config");

/**
 * 获取所有分类
 */
/* exports.getCategory = async (req, res) => {
  const { page = 1, size = 5, article_title } = req.query;
  // 计算偏移量
  const offset = (page - 1) * size;
  let sql = `select art.*,sort.name from blog_article art,blog_sort sort 
  where art.category_id=sort.id limit ${offset},${size}`;
  const sql1 = `select art.*,sort.name from blog_article art,blog_sort sort 
  where art.category_id=sort.id and art.article_title like '%${article_title}%' limit ${offset},${size}`;
  if (article_title != "") {
    var [rows] = await db.promise().query(sql1);
    sql = `select count(*) as total from (${sql1}) middle_table`;
  } else {
    var [rows] = await db.promise().query(sql);
    sql = `select count(*) as total from blog_article`;
  }
  // 计算总数
  const [result] = await db.promise().query(sql);
  if (rows.length != 0 && result.length != 0) {
    var { total } = result[0];
    // console.log(total);
    res.send(info(0, "获取所有文章成功", { total, rows }));
  } else {
    res.send(info(0, "暂无数据", []));
  }
}; */

//获取全部
exports.getCategorys = async (req, res) => {
  let sql = `select category_id,count(*) num,sum(article_views) sum from blog_article group by category_id`;
  let test = await db.promise().query(sql);
  // console.log(test);
  test.forEach(async (item) => {
    sql = `update blog_sort set sort_articles = '${item.num}',sort_views = '${item.sum}' where id='${item.category_id}'`;
    await db.promise().query(sql);
  });

  // 操作数据库
  sql = `select id, name, sort_updatedTime, sort_articles, sort_views, sort_praise from blog_sort  where sort_deletedTime is Null`;
  const data = await db.promise().query(sql);
  res.send({
    code: 0,
    message: "获取所有用户成功",
    result: {
      data,
    },
  });
};

/**
 * 根据id获取单个用户数据
 * GET /users/:id
 */
exports.getCategory = async (req, res) => {
  const { id } = req.params;

  let sql = `select id, name from blog_sort where id=${id}`;
  const data = await db.promise().query(sql);
  console.log(data);
  res.send({
    code: 0,
    message: "获取用户成功",
    result: data,
  });
};

//增加分类
exports.addCategory = async (req, res) => {
  const { name } = req.body;
  console.log(name);
  let sql = `select name from blog_sort where name='${name}' and sort_deletedTime is null`;
  const result = await db.promise().query(sql);
  // console.log(result);
  // console.log(result[0].length);

  //判断此分类名是否存在
  if (result[0].length != 0) {
    res.send({
      code: 1,
      message: "分类名重复",
      result: {
        name,
      },
    });
  } else {
    sql = `insert into blog_sort (name) values ('${name}')`;
    const { insertId } = await db.promise().query(sql);

    res.send({
      code: 0,
      message: "添加分类成功",
      result: {
        id: insertId,
        name,
      },
    });
  }
};

//修改分类
exports.changeCategory = async (req, res) => {
  const { id } = req.params;
  const { name } = req.body;
  // console.log(id);
  let sql = `update blog_sort set name='${name}' where id=${id}`;
  await db.promise().query(sql);

  res.send({
    code: 0,
    message: "修改成功",
    result: {
      id,
      name,
    },
  });
};

//删除单个
exports.deleteCategory = async (req, res) => {
  const { id } = req.params;
  // console.log(id);
  let sql = `update blog_sort set sort_deletedTime=NOW() where id=${id}`;
  await db.promise().query(sql);

  res.send({
    code: 0,
    message: "删除成功",
    result: {
      id,
    },
  });
};

//批量删除
exports.deleteCategorys = async (req, res) => {
  const delIds = req.body;
  // console.log(params);
  for (let id of delIds) {
    // console.log(id);
    let sql = `update blog_sort set sort_deletedTime=NOW() where id=${id}`;
    await db.promise().query(sql);
  }
  res.send({
    code: 0,
    message: "删除成功",
    result: {
      delIds,
    },
  });
};

/**
 * 获取所有分类名称
 */
exports.getCategoryName = async (req, res) => {
  let sql = `select id,name from blog_sort where sort_deletedTime is null`;
  const [rows] = await db.promise().query(sql);
  if (rows.length != 0) {
    res.send(info(0, "获取分类名称成功", rows));
  }
};
/**
 * 删除分类
 */
/* exports.deleteArticle = async (req, res) => {
  const { ids } = req.body;
  for (let i = 0; i < ids.length; i++) {
    let sql = `delete from blog_article where id=${ids[i]}`;
    var [rows] = await db.promise().query(sql);
  }
  res.send(info(0, "删除文章成功", ""));
}; */
